package com.nus.servlets;

import com.nus.beans.Project;
import com.nus.beans.ProjectRemark;
import com.nus.beans.User;
import com.nus.database.ProjectsDBManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 11/1/12
 * Time: 10:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class ViewProjectRemarksServlet extends AbstractServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Connection connection = getUserDatabaseConnection(request);
        ProjectsDBManager projectManager = new ProjectsDBManager(connection);

        String projectIdParam = request.getParameter("projects");
        int projectIdInt;
        if(projectIdParam == null){
            projectIdInt = (Integer) request.getAttribute("projects");
        } else {
            projectIdInt = Integer.parseInt(projectIdParam);
        }

        Project project = projectManager.getProject(projectIdInt);
        request.getSession().setAttribute("project", project);


        List<ProjectRemark> projectRemarksList = projectManager.getProjectRemarks(projectIdInt);

        projectManager.close();

        request.setAttribute("projectRemarks", projectRemarksList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/view_project_remarks.jsp");
        dispatcher.forward(request, response);
    }
}
